package com.navinfo.tripanalysis.common.arithmetic;

import com.navinfo.tripanalysis.common.arithmetic.annotation.Arithmetic;
import com.navinfo.tripanalysis.common.arithmetic.common.CommonData;
import com.navinfo.tripanalysis.common.arithmetic.common.OutStatisticsRangeData.RangeDiffGasOpenPercentStatisticsData;
import com.navinfo.tripanalysis.common.arithmetic.common.OuterStatisticData;
import com.navinfo.tripanalysis.common.arithmetic.common.VarCommon;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarRangeGasOpenPercent;
import com.navinfo.tripanalysis.common.arithmetic.custom.VarRangeGasOpenPercentTest;
import com.navinfo.tripanalysis.common.arithmetic.enums.RangeGasOpenPercentChangeEnum;
import com.navinfo.tripanalysis.common.arithmetic.enums.RangeGasOpenPercentEnum;
import com.navinfo.tripanalysis.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.tripanalysis.common.util.ArithmeticUtils;
import com.navinfo.tripanalysis.common.util.BigDecimalUtils;

import java.util.Optional;

/**
 * 油门开合度算法.<br>
 * 依赖{@link IdleParking}的怠速状态.<br>
 * 思路是累加油门开合度，累计数量，行程结束的时候除一下<br>
 * 输出：统计<br>
 */
@Arithmetic(name = "平均油门开度")
public class RangeGasOpenPercentTest extends AbstractArithmetic<VarRangeGasOpenPercentTest> {

    @Override
    public void calculate() {
        logger.debug("进入平均油门开度算法.");
        VarRangeGasOpenPercentTest var = super.getObj();
        CommonData commonData = drivingData.getCommonData();

        int dffTime = commonData.getDffTime();
        double dffDistance = commonData.getDffMileage();
        double dffOil = commonData.getDffFuel();

        int lastAccPedalPos = (int)commonData.getLastAcceleratorPosition();
        int accPedalPos = (int)commonData.getAcceleratorPosition();

        //每秒油门变化
        int dffaccPedalPos = BigDecimalUtils.divide((accPedalPos - lastAccPedalPos),dffTime/1000,0).intValue();

        //统计油门开度区间里程、油耗、时长
        VarCommon varCommon = null;
        if (commonData.isWork()) {
            if (RangeGasOpenPercentEnum.RANGE_0.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent0();
            } else if (RangeGasOpenPercentEnum.RANGE_10.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent10();
            } else if (RangeGasOpenPercentEnum.RANGE_20.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent20();
            } else if (RangeGasOpenPercentEnum.RANGE_30.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent30();
            } else if (RangeGasOpenPercentEnum.RANGE_40.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent40();
            } else if (RangeGasOpenPercentEnum.RANGE_50.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent50();
            } else if (RangeGasOpenPercentEnum.RANGE_60.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent60();
            } else if (RangeGasOpenPercentEnum.RANGE_70.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent70();
            } else if (RangeGasOpenPercentEnum.RANGE_80.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent80();
            } else if (RangeGasOpenPercentEnum.RANGE_90.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent90();
            } else if (RangeGasOpenPercentEnum.RANGE_99.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent99();
            } else if (RangeGasOpenPercentEnum.RANGE_100.getRange().contains(accPedalPos)) {
                varCommon = var.getGasOpenPercent100();
            }

            if(varCommon != null){
                varCommon.addMileage(dffDistance);
                varCommon.addFuel(dffOil);
                varCommon.addDuration(dffTime);
            }
        }

        var.setGasOpenPercentSum(var.getGasOpenPercentSum() + accPedalPos);
        var.setGasOpenPercentCount(var.getGasOpenPercentCount() + 1);

        //统计油门开度每秒变化区间里程、油耗、时长
        VarCommon varCommon1 = null;
        if(RangeGasOpenPercentChangeEnum.RANGE_F50.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentF50();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_F40.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentF40();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_F30.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentF30();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_F20.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentF20();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_F10.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentF10();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_0.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercent0();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_eq_0.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentEq0();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_10.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercent10();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_20.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercent20();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_30.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercent30();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_40.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercent40();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_50.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercent50();
        }else if(RangeGasOpenPercentChangeEnum.RANGE_Max.getRange().contains(dffaccPedalPos)){
            varCommon1 = var.getDiffGasOpenPercentMax();
        }
        if(varCommon1 != null){
            varCommon1.addMileage(dffDistance);
            varCommon1.addFuel(dffOil);
            varCommon1.addDuration(dffTime);
        }


        logger.debug("平均油门开度算法结束.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeGasOpenPercentTest var = super.getObj();

        VarCommon gasOpenPercent0 = var.getGasOpenPercent0();
        statisticDataOut.setGasOpenPercent0Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent0.getDuration()));
        statisticDataOut.setGasOpenPercent0Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent0.getMileage()));
        statisticDataOut.setGasOpenPercent0Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent0.getFuel()));

        VarCommon gasOpenPercent10 = var.getGasOpenPercent10();
        statisticDataOut.setGasOpenPercent10Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent10.getDuration()));
        statisticDataOut.setGasOpenPercent10Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent10.getMileage()));
        statisticDataOut.setGasOpenPercent10Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent10.getFuel()));

        VarCommon gasOpenPercent20 = var.getGasOpenPercent20();
        statisticDataOut.setGasOpenPercent20Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent20.getDuration()));
        statisticDataOut.setGasOpenPercent20Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent20.getMileage()));
        statisticDataOut.setGasOpenPercent20Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent20.getFuel()));

        VarCommon gasOpenPercent30 = var.getGasOpenPercent30();
        statisticDataOut.setGasOpenPercent30Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent30.getDuration()));
        statisticDataOut.setGasOpenPercent30Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent30.getMileage()));
        statisticDataOut.setGasOpenPercent30Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent30.getFuel()));

        VarCommon gasOpenPercent40 = var.getGasOpenPercent40();
        statisticDataOut.setGasOpenPercent40Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent40.getDuration()));
        statisticDataOut.setGasOpenPercent40Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent40.getMileage()));
        statisticDataOut.setGasOpenPercent40Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent40.getFuel()));

        VarCommon gasOpenPercent50 = var.getGasOpenPercent50();
        statisticDataOut.setGasOpenPercent50Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent50.getDuration()));
        statisticDataOut.setGasOpenPercent50Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent50.getMileage()));
        statisticDataOut.setGasOpenPercent50Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent50.getFuel()));

        VarCommon gasOpenPercent60 = var.getGasOpenPercent60();
        statisticDataOut.setGasOpenPercent60Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent60.getDuration()));
        statisticDataOut.setGasOpenPercent60Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent60.getMileage()));
        statisticDataOut.setGasOpenPercent60Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent60.getFuel()));

        VarCommon gasOpenPercent70 = var.getGasOpenPercent70();
        statisticDataOut.setGasOpenPercent70Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent70.getDuration()));
        statisticDataOut.setGasOpenPercent70Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent70.getMileage()));
        statisticDataOut.setGasOpenPercent70Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent70.getFuel()));

        VarCommon gasOpenPercent80 = var.getGasOpenPercent80();
        statisticDataOut.setGasOpenPercent80Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent80.getDuration()));
        statisticDataOut.setGasOpenPercent80Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent80.getMileage()));
        statisticDataOut.setGasOpenPercent80Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent80.getFuel()));

        VarCommon gasOpenPercent90 = var.getGasOpenPercent90();
        statisticDataOut.setGasOpenPercent90Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent90.getDuration()));
        statisticDataOut.setGasOpenPercent90Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent90.getMileage()));
        statisticDataOut.setGasOpenPercent90Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent90.getFuel()));

        VarCommon gasOpenPercent99 = var.getGasOpenPercent99();
        statisticDataOut.setGasOpenPercent99Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent99.getDuration()));
        statisticDataOut.setGasOpenPercent99Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent99.getMileage()));
        statisticDataOut.setGasOpenPercent99Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent99.getFuel()));

        VarCommon gasOpenPercent100 = var.getGasOpenPercent100();
        statisticDataOut.setGasOpenPercent100Duration(ArithmeticUtils.timeMsToSecond(gasOpenPercent100.getDuration()));
        statisticDataOut.setGasOpenPercent100Mileage(ArithmeticUtils.mileageMToM(gasOpenPercent100.getMileage()));
        statisticDataOut.setGasOpenPercent100Fuel(ArithmeticUtils.fuelMlToMl(gasOpenPercent100.getFuel()));

        statisticDataOut.setGasOpenPercentSum((long)var.getGasOpenPercentSum());
        statisticDataOut.setGasOpenPercentCount(var.getGasOpenPercentCount());

        //统计油门开度每秒变化区间里程、油耗、时长
        RangeDiffGasOpenPercentStatisticsData rangeDiffGasOpenPercentStatisticsData = new RangeDiffGasOpenPercentStatisticsData();

        VarCommon diffGasOpenPercentF50 = var.getDiffGasOpenPercentF50();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF50_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentF50.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF50_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentF50.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF50_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentF50.getFuel()));

        VarCommon diffGasOpenPercentF40 = var.getDiffGasOpenPercentF40();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF40_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentF40.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF40_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentF40.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF40_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentF40.getFuel()));

        VarCommon diffGasOpenPercentF30 = var.getDiffGasOpenPercentF30();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF30_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentF30.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF30_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentF30.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF30_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentF30.getFuel()));

        VarCommon diffGasOpenPercentF20 = var.getDiffGasOpenPercentF20();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF20_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentF20.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF20_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentF20.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF20_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentF20.getFuel()));

        VarCommon diffGasOpenPercentF10 = var.getDiffGasOpenPercentF10();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF10_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentF10.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF10_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentF10.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeF10_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentF10.getFuel()));

        VarCommon diffGasOpenPercent0 = var.getDiffGasOpenPercent0();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange0_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercent0.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange0_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercent0.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange0_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercent0.getFuel()));

        VarCommon diffGasOpenPercentEq0 = var.getDiffGasOpenPercentEq0();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeEq0_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentEq0.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeEq0_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentEq0.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeEq0_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentEq0.getFuel()));

        VarCommon diffGasOpenPercent10 = var.getDiffGasOpenPercent10();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange10_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercent10.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange10_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercent10.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange10_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercent10.getFuel()));

        VarCommon diffGasOpenPercent20 = var.getDiffGasOpenPercent20();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange20_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercent20.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange20_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercent20.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange20_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercent20.getFuel()));

        VarCommon diffGasOpenPercent30 = var.getDiffGasOpenPercent30();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange30_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercent30.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange30_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercent30.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange30_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercent30.getFuel()));

        VarCommon diffGasOpenPercent40 = var.getDiffGasOpenPercent40();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange40_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercent40.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange40_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercent40.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange40_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercent40.getFuel()));

        VarCommon diffGasOpenPercent50 = var.getDiffGasOpenPercent50();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange50_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercent50.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange50_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercent50.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRange50_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercent50.getFuel()));

        VarCommon diffGasOpenPercentMax = var.getDiffGasOpenPercentMax();
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeMax_duration(ArithmeticUtils.timeMsToSecond(diffGasOpenPercentMax.getDuration()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeMax_mileage(ArithmeticUtils.mileageMToM(diffGasOpenPercentMax.getMileage()));
        rangeDiffGasOpenPercentStatisticsData.setDiffGasOpenPercentRangeMax_fuel(ArithmeticUtils.fuelMlToMl(diffGasOpenPercentMax.getFuel()));

        statisticDataOut.setRangeDiffGasOpenPercentStatisticsData(rangeDiffGasOpenPercentStatisticsData);
    }
}
